python 对字符串切片及翻转
给定一个字符串,从头部或尾部截取指定数量的字符串,然后将其翻转拼接。
实例
def rotate(input,d):
lfirst = input[0 : d]
lsecond = input[d :]
rfirst = input[0 : len(input)-d]
rsecond = input[len(input)-d : ]
print( "头部切片翻转 : ", (lsecond + lfirst) ) print( "尾部切片翻转 : ", (rsecond + rfirst) ) if __name__ == "__main__":
input = 'runoob' d=2 # 截取两个字符 rotate(input,d)
执行以上代码输出结果为:
头部切片翻转 : noobru 尾部切片翻转 : obruno
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!